#!/bin/bash
time=$(date "+%Y-%m-%d %H:%M:%S")
echo "currentTime ${time}"
echo "JENKINS_HOME = ${JENKINS_HOME} WORKSPACE = ${WORKSPACE}"
JAR_NAME=${JOB_NAME}.jar
JAR_PATH="${WORKSPACE}/target/${JAR_NAME}"
echo "JAR_NAME = ${JAR_NAME} JAR_PATH = ${JAR_PATH}"
echo "user name = ${USER}"
#存在进程杀死
pid=$(ps -ef|grep ${JAR_NAME}|grep -v grep|awk '{print $2}')
echo $pid
if [[ $pid -eq 0 ]]; then
   echo "${JAR_NAME} is not running."
else
   echo "${JAR_NAME} PID=${pid} is running. begin restart."
   kill -9 $pid
fi
#启动
nohup java -jar $JAR_PATH & >/dev/null 2>&1 &
newPid=$(ps -ef|grep ${JAR_NAME}|grep -v grep|awk '{print $2}')
echo ">>> start $JAR_NAME successed PID = $newPid <<<"